﻿using Li.EntityFramework.IRepository;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Li.EntityFramework.Repository
{
    public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class
    {
        protected DbContext db;
        public BaseRepository()
        {
            db = DBContextFactory.CreateDbContext();
        }

        public void Add(TEntity entity)
        {
            db.Set<TEntity>().Add(entity);
        }

        public int DBSaveChange() => db.SaveChanges();

        public IEnumerable<TEntity> GetALL()
        {
            return db.Set<TEntity>();
        }

        public TEntity GetById(int id)
        {
            return db.Set<TEntity>().Find(id);
        }

        public void Remove(TEntity entity)
        {
            db.Set<TEntity>().Remove(entity);
        }
    }
}
